package top.zy.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import top.zy.common.zymallcommon.utils.exception.ZymalException;
import top.zy.common.zymallcommon.vo.PageResult;
import top.zy.search.client.CategoryClient;
import top.zy.search.client.GoodsClient;
import top.zy.search.dto.Goods;
import top.zy.search.dto.SearchRequest;
import top.zy.search.service.SearchService;
import top.zy.search.vo.SearchVo;
import top.zy.service.zymallgoodsinterface.vo.GoodsVo;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 11:11 2019/7/4
 */
@RefreshScope
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

    @Value("${redis.host}")
    private String redisHost;
    @Autowired
    private GoodsClient goodsClient;

    @PostMapping("page")
    public SearchVo searchPage(@RequestBody SearchRequest searchRequest){
      return searchService.searchPage(searchRequest);
    }

    @RequestMapping("main")
    public String main() {
        return redisHost;
    }
    @GetMapping("es")
    public void  pushEs(){
        PageResult<GoodsVo> pageResult= goodsClient.queryGoodsPage(1,500,null,true,null);
        List<GoodsVo> items = pageResult.getItems();
        if (CollectionUtils.isEmpty(items)){
            throw new ZymalException("数据为空");
        }
        Integer id=Integer.MAX_VALUE;
        for (int i = 49934; i < id; i++) {
            Goods goods=searchService.buildGoods(items.get(0));
            goods.setId(Long.valueOf(i));
            searchService.saveGoodsTOEs(goods);
        }
    }
}
